package com.qing.springcloud.dao;


import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import com.qing.springcloud.entity.UserFriend;
import com.qing.springcloud.mapper.UserFriendMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * (user_friend)数据DAO
 *
 * @author kancy
 * @since 2025-07-09 19:32:19
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class UserFriendDao extends ServiceImpl<UserFriendMapper, UserFriend> {

    public boolean existsByUserIdAndFriendId(@NotNull(message = "发起人ID不能为空") Long fromUserId, @NotNull(message = "接收人ID不能为空") Long toUserId) {
        return lambdaQuery().eq(UserFriend::getUserId, fromUserId).eq(UserFriend::getFriendId, toUserId).exists();
    }

    public void deleteByUserIdAndFriendId(Long userId, Long friendId) {
        lambdaUpdate().eq(UserFriend::getUserId, userId).eq(UserFriend::getFriendId, friendId).remove();
    }

    public List<Long> findFriendIdsByUserId(Long userId,Integer type) {
        return lambdaQuery().eq(UserFriend::getUserId, userId).select(UserFriend::getFriendId).eq(UserFriend::getRelationType, type).list().stream().map(UserFriend::getFriendId).collect(Collectors.toList());
    }

    public Optional<UserFriend> findByUserIdAndFriendId(Long userId, Long friendId) {
        return lambdaQuery().eq(UserFriend::getUserId, userId).eq(UserFriend::getFriendId, friendId).oneOpt();
    }
}